package sk.gtug.pivoplus.malepivo.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import sk.gtug.pivoplus.malepivo.model.GeocodeResponseGeometry.Bounds;
import android.content.ContentValues;
import android.database.Cursor;

public class GeocodeResponseResult {
		
    public interface GeocodeResponseResultColumns {
        String ADMINISTRATIVE_AREA_LEVEL_1 = "administrative_area_level_1";
        String ADMINISTRATIVE_AREA_LEVEL_2 = "administrative_area_level_2";
        String ADMINISTRATIVE_AREA_LEVEL_3 = "administrative_area_level_3";
        String FORMATTED_ADDRESS = "formatted_address";
        String LOCALITY = "locality";
        String SUBLOCALITY = "sublocality";
        String COUNTRY = "country";
        String COUNTRY_NAME = "country_name";
        String COUNTRY_CODE = "country_code";
        String LOCATION_LAT = "location_lat";
        String LOCATION_LNG = "location_lng";
        String LOCATION_TYPE = "location_type";
        String VIEWPORT_SW_LAT = "viewport_sw_lat";
        String VIEWPORT_SW_LNG = "viewport_sw_lng";
        String VIEWPORT_NE_LAT = "viewport_ne_lat";
        String VIEWPORT_NE_LNG = "viewport_ne_lng";
        String POSTAL_CODE = "postal_code";		
    }
		
		//@SerializedName("max_id")
		public List<String> types;
		
		public static class Country {
			public String name, code;
		}
		
		public Country country;
		
		public String formatted_address;
			
		public List<AdressComponents> address_components;
		
//		public AdressComponents getAdministrativeArea() {
//			AdressComponents result = null;
//			result = getAdressComponentType(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_1);
//			if (result != null) 
//				return result;
//			
//			result = getAdressComponentType(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_2);
//			if (result != null) 
//				return result;
//			
//			result = getAdressComponentType(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_3);
//			if (result != null) 
//				return result;
//			
//			return null;
//		}
		
		public String getAdressComponentType(String type){
			for (AdressComponents component : address_components){
				//if (component.isType(type))
				if (component.types.contains(type))
					return component.long_name;
			}			
			return null;
		}
		
		public static class AdressComponents {
			public List<String> types;
			public String long_name;
			public String short_name;
			
//			public boolean isType(String type){
//				for(String t : types){
//					if (t.equalsIgnoreCase(type))
//						return true;
//				}
//				return false;
//			}
		}
				
		public GeocodeResponseGeometry geometry;		
		
		@Override
		public String toString(){
			return (formatted_address != null) ? formatted_address : "No location";
		}
		
		public static ContentValues buildContentValues(GeocodeResponseResult result)
        {
			if (result.country == null) result.country = new GeocodeResponseResult.Country();
			
        	ContentValues tmp = new ContentValues();
        	tmp.put(GeocodeResponseResultColumns.FORMATTED_ADDRESS, result.formatted_address);
        	tmp.put(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_1, result.getAdressComponentType(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_1));
        	tmp.put(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_2, result.getAdressComponentType(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_2));
        	tmp.put(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_3, result.getAdressComponentType(GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_3));
        	tmp.put(GeocodeResponseResultColumns.LOCALITY, result.getAdressComponentType(GeocodeResponseResultColumns.LOCALITY));
        	tmp.put(GeocodeResponseResultColumns.SUBLOCALITY, result.getAdressComponentType(GeocodeResponseResultColumns.SUBLOCALITY));
        	tmp.put(GeocodeResponseResultColumns.COUNTRY, result.getAdressComponentType(GeocodeResponseResultColumns.COUNTRY));
        	tmp.put(GeocodeResponseResultColumns.COUNTRY_NAME, result.country.name);
        	tmp.put(GeocodeResponseResultColumns.COUNTRY_CODE, result.country.code);
        	tmp.put(GeocodeResponseResultColumns.POSTAL_CODE, result.getAdressComponentType(GeocodeResponseResultColumns.POSTAL_CODE));
        	tmp.put(GeocodeResponseResultColumns.LOCATION_TYPE, result.geometry.location_type);
        	//pretypovanie kvoli kompatibilite s MediaStore.Video.Media
        	tmp.put(GeocodeResponseResultColumns.LOCATION_LAT, String.valueOf(Double.parseDouble(result.geometry.location.lat.replaceAll("0+$", ""))));
        	tmp.put(GeocodeResponseResultColumns.LOCATION_LNG, String.valueOf(Double.parseDouble(result.geometry.location.lng.replaceAll("0+$", "")))); 
        	tmp.put(GeocodeResponseResultColumns.VIEWPORT_SW_LAT, String.valueOf(Double.parseDouble(result.geometry.viewport.southwest.lat.replaceAll("0+$", ""))));
        	tmp.put(GeocodeResponseResultColumns.VIEWPORT_SW_LNG, String.valueOf(Double.parseDouble(result.geometry.viewport.southwest.lng.replaceAll("0+$", ""))));
        	tmp.put(GeocodeResponseResultColumns.VIEWPORT_NE_LAT, String.valueOf(Double.parseDouble(result.geometry.viewport.northeast.lat.replaceAll("0+$", ""))));
        	tmp.put(GeocodeResponseResultColumns.VIEWPORT_NE_LNG, String.valueOf(Double.parseDouble(result.geometry.viewport.northeast.lng.replaceAll("0+$", ""))));
 
//        	ContentValues values = new ContentValues();
//        	
//        	for(Entry<String, Object> item : tmp.valueSet()) {
//        		Log.d("GeoResult", item.getKey() + ", " + item.getValue());
//        		if (item.getValue() != null)
//        			values.put(item.getKey(), item.getValue());      
//        		
//        	}
        	
        	Entry<String, Object> entry;
        	for (Iterator<Entry<String, Object>> it = tmp.valueSet().iterator(); it.hasNext(); )
    		{
    			entry = it.next();
    			if (entry.getValue() == null)
    				it.remove();
    		}
        	
    	    return tmp;
        }
		
		public static GeocodeResponseResult buildGeocodeResponseResult(Cursor cursor){
			GeocodeResponseResult result = new GeocodeResponseResult();
			AdressComponents adressComponent;
						
			result.address_components = new ArrayList<AdressComponents>();
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_1);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_2);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.ADMINISTRATIVE_AREA_LEVEL_3);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.LOCALITY);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.SUBLOCALITY);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.COUNTRY);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			adressComponent = buildAdressComponent(cursor, GeocodeResponseResultColumns.POSTAL_CODE);
			if (adressComponent != null)
				result.address_components.add(adressComponent);
			
			result.formatted_address = cursor.getString(cursor.getColumnIndex(GeocodeResponseResultColumns.FORMATTED_ADDRESS));
			result.geometry = new GeocodeResponseGeometry();			
			result.geometry.location_type = getString(cursor, GeocodeResponseResultColumns.LOCATION_TYPE);
			result.geometry.location = new GeoLocation();
			result.geometry.location.lat = getString(cursor, GeocodeResponseResultColumns.LOCATION_LAT);
			result.geometry.location.lng = getString(cursor, GeocodeResponseResultColumns.LOCATION_LNG);
			result.geometry.viewport = new Bounds();
			result.geometry.viewport.northeast = new GeoLocation();
			result.geometry.viewport.southwest = new GeoLocation();
			result.geometry.viewport.southwest.lat = getString(cursor, GeocodeResponseResultColumns.VIEWPORT_SW_LAT);
			result.geometry.viewport.southwest.lng = getString(cursor, GeocodeResponseResultColumns.VIEWPORT_SW_LNG);
			result.geometry.viewport.northeast.lat = getString(cursor, GeocodeResponseResultColumns.VIEWPORT_NE_LAT);
			result.geometry.viewport.northeast.lng = getString(cursor, GeocodeResponseResultColumns.VIEWPORT_NE_LNG);

			return result;
		}
		
		private static String getString(Cursor cursor, String column) {
			int index = cursor.getColumnIndex(column);
			if (index == -1) return null;
			return cursor.getString(index);
		}
		
		private static AdressComponents buildAdressComponent(Cursor cursor, String type) {
			AdressComponents adressComponent = new AdressComponents();
			if (getString(cursor, type) != null)
			adressComponent.long_name = getString(cursor, type);
			adressComponent.types = new ArrayList<String>();
			adressComponent.types.add(type);
			return adressComponent;
		}
}
 